package com.zyx.flinkdemo.stream.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @author Yaxi.Zhang
 * @since 2021/3/30 14:46
 */
public class TimeUtils {

    public static String getHhmmss(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String str = sdf.format(new Date(time));
        return "时间:" + str;
    }

    public static String getFormatTimeStr(long ts) {
        Instant ins = Instant.ofEpochMilli(ts);
        ZonedDateTime zdt = ins.atZone(ZoneId.systemDefault());
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dtf.format(zdt);
    }

    public static long getDiffDays(String oDateStr, String cDateStr) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate oDate = LocalDate.parse(oDateStr, dtf);
        LocalDate cDate = LocalDate.parse(cDateStr, dtf);
        LocalDateTime cDateTime = LocalDateTime.of(cDate, LocalTime.of(0, 0, 0));
        LocalDateTime oDateTime = LocalDateTime.of(oDate, LocalTime.of(0, 0, 0));
        return Duration.between(oDateTime, cDateTime).toDays();
    }

    public static Timestamp stringToTime(String dateString) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        dateFormat.setLenient(false);
        Date timeDate = dateFormat.parse(dateString);
        return new Timestamp(timeDate.getTime());
    }

    public static void main(String[] args) {
        System.out.println(getDiffDays("20210622", "20210625"));
    }

}
